package com.appspot.yourvirtualgymtrainer;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class UtilYoman {
	static User getUser(HttpServletRequest req, HttpServletResponse resp) {
	    UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();
	    PrintWriter out;
		try {
			out = resp.getWriter();
		    if (user != null) {
				System.out.println(user.getNickname());
			    out.println("zalogowany");
		        resp.getWriter().println("Hello, " + user.getNickname());
		        return user;
		    } else {
		        resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
		    } 
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
